package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.UsersDao;
import entity.Users;

public class UsersDaoImpl extends BaseDaoImpl<Users> implements UsersDao{

	@Override
	public Users getUserByName(String username) {
		String sql = "SELECT * FROM Users where UserName =?";
		List<Users> list = showAll(sql,username);
		return list.size()>0?list.get(0):null;
	}

	@Override
	public List<Users> listByRs(ResultSet rs) {
		List<Users> list =new ArrayList<Users>();
		try {
			while(rs.next()){
				list.add(new Users(rs.getInt("id"),
						rs.getString("UserName"),
						rs.getString("PASSWORD"),
						rs.getDate("LastLoginTime"),
						rs.getInt("IsAdmin")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public void addUers(Users u) {
		String sql = "INSERT INTO Users(UserName,PASSWORD,LastLoginTime,IsAdmin) VALUES(?,?,?,?);";
		CUDObject(sql, u.getUsername(),u.getPassword(),u.getLastlogintime(),u.getIsadmin());
	}

	@Override
	public Users checklogin(String username, String password) {
		String sql = "SELECT * FROM Users where UserName =? and PASSWORD=?";
		return showAll(sql,username,password).size()!=0?showAll(sql,username,password).get(0):null;
	}

}
